# encoding:utf-8

import RPi.GPIO as GPIO
import config as c
import time

class Dist(object):
	def __init__(self):
		GPIO.setmode(GPIO.BCM)
		GPIO.setwarnings(False)
		#3号脚，GPIO2
		GPIO.setup(c.TRLG,GPIO.OUT,initial=GPIO.LOW)
		#5号脚，GPIO3
		GPIO.setup(c.ECHO,GPIO.IN)
	def checkdistance():
		#发出触发信号
		GPIO.output(c.TRLG,GPIO.HIGH)
		#保持15us
		time.sleep(0.000015)
		GPIO.output(c.TRLG,GPIO.LOW)
		while not GPIO.input(c.ECHO):
			pass
		#发现高电平时开始计时
		t1 = time.time()
		while GPIO.input(c.ECHO):
			pass
		#高电平时结束计时
		t2 = time.time()
		#返回距离米
		return (t2 - t1)*340/2

